package Class.Dialog;

import Class.Handler.*;
import Class.Dialog.*;
import Class.TextWindow.*;
import Class.Main.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TypeFaceDialog extends JDialog implements ActionListener {

    private TextWindow textWindow;

    private JComboBox<String> fontFamilyComboBox;
    private JComboBox<String> fontStyleComboBox;
    private JComboBox<Integer> fontSizeComboBox;

    private  JButton confirmButton = new JButton("确认");
    private  JButton cancelButton = new JButton("取消");


    public TypeFaceDialog(TextWindow textWindow){
        super(textWindow,"字体",true);
        this.textWindow=textWindow;

        //建立框体，使用绝对布局，需指定坐标
        JPanel jPanel = new JPanel(null);

        //添加各项组件
        //获取系统字体名称
        GraphicsEnvironment localGraphicsEnviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] avaliableFontFamilyNames = localGraphicsEnviroment.getAvailableFontFamilyNames();
        fontFamilyComboBox = new JComboBox<>(avaliableFontFamilyNames);
        jPanel.add(fontFamilyComboBox);

        String[] avaliableFontStyleNames = {"Plain(常规)","Bold(粗体)","Italic(斜体)","BoldAndItalic(粗偏斜体)"};
        fontStyleComboBox = new JComboBox<>(avaliableFontStyleNames);
        jPanel.add(fontStyleComboBox);

        Integer[] avaliableFontSizeNums = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
        fontSizeComboBox = new JComboBox<>(avaliableFontSizeNums);
        jPanel.add(fontSizeComboBox);

        confirmButton.addActionListener(this);
        jPanel.add(confirmButton);

        cancelButton.addActionListener(this);
        jPanel.add(cancelButton);

        //组装组件
        fontFamilyComboBox.setBounds(20,20,220,40);
        fontStyleComboBox.setBounds(260,20,160,40);
        fontSizeComboBox.setBounds(440,20,80,40);
        confirmButton.setBounds(300,100,100,35);
        cancelButton.setBounds(430,100,100,35);

        Font fontNow = textWindow.getTextArea().getFont();

        String fontFamilyNow = (String) fontNow.getFamily();
        String fontStyleNowStr = "";
        int fontStyleNow = (Integer) fontNow.getStyle();
        Integer fontSizeNow = (Integer) fontNow.getSize();

        if( fontStyleNow == Font.PLAIN){
            fontStyleNowStr = "Plain(常规)";
        } else if (fontStyleNow == Font.BOLD) {
            fontStyleNowStr = "Bold(粗体)";
        } else if (fontStyleNow == Font.ITALIC) {
            fontStyleNowStr = "Italic(斜体)";
        } else if (fontStyleNow == Font.BOLD + Font.ITALIC) {
            fontStyleNowStr = "BoldAndItalic(粗偏斜体)";
        }

        System.out.println(fontFamilyNow);
        System.out.println(fontStyleNow);
        System.out.println(fontStyleNowStr);
        System.out.println(fontSizeNow);

        fontFamilyComboBox.setSelectedItem(fontFamilyNow);
        fontStyleComboBox.setSelectedItem(fontStyleNowStr);
        fontSizeComboBox.setSelectedItem(fontSizeNow);

        this.getContentPane().add(jPanel);
        setSize(550,180);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cancelButton)
            this.dispose();
        else if (e.getSource() == confirmButton) {

            
            String fontFamily = (String) fontFamilyComboBox.getSelectedItem();
            String fontStyleStr = (String) fontStyleComboBox.getSelectedItem();
            Integer fontSize = (Integer) fontSizeComboBox.getSelectedItem();
            int fontStyle=0;
            if("Plain(常规)".equals(fontStyleStr)){
                fontStyle = Font.PLAIN;
            } else if ("Bold(粗体)".equals(fontStyleStr)) {
                fontStyle = Font.BOLD;
            } else if ("Italic(斜体)".equals(fontStyleStr)) {
                fontStyle = Font.ITALIC;
            } else if ("BoldAndItalic(粗偏斜体)".equals(fontStyleStr)) {
                fontStyle = Font.BOLD + Font.ITALIC;
            }
            Font font = new Font(fontFamily,fontStyle,fontSize);
            textWindow.getTextArea().setFont(font);
            this.dispose();
        }
    }
}
